﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;
using System.Drawing.Text;

using permit.App_Code.DataAccess;
using permit.App_Code;
using permit.App_Code.Entity;

namespace permit.print
{
    public partial class PrintParameter : System.Web.UI.Page
    {
        PrinterAccess prtAcc = new PrinterAccess();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlPrintBind();
                InstalledFontCollection fonts = new InstalledFontCollection();


                ddlFont.Items.Clear();
                ListItem li = new ListItem("默认", "0");
                ddlFont.Items.Add(li);
                foreach (System.Drawing.FontFamily family in fonts.Families)
                {
                    ddlFont.Items.Add(family.Name);

                }
            }
        }

        protected void btnAddPrinter_Click(object sender, EventArgs e)
        {
            if (txtPriName.Text == "")
            {
                Page.RegisterStartupScript("this", "<script>alert('打印机名称不能为空')</script>");

                return;
            }
            if (prtAcc.IsPrintName(txtPriName.Text))
            {
                Page.RegisterStartupScript("name", "<script>alert('打印机名称已经存在')</script>");
                return;
            }
            else
            {
                if (prtAcc.InsertPrinter(txtPriName.Text, ((UserTable)Session["UserTable"]).DEP_ID) <= 0)
                {
                    Page.RegisterStartupScript("name1", "<script>alert('添加失败，请重试！')</script>");
                    return;
                }
                ddlPrinter.Items.Add(new ListItem(txtPriName.Text, txtPriName.Text));
                Page.RegisterStartupScript("name2", "<script>alert('添加成功')</script>");



            }
        }

        public void ddlPrintBind()
        {
            DataSet ds = prtAcc.GetPrintName(((UserTable)Session["UserTable"]).DEP_ID);


            if (ds != null || ds.Tables[0].Rows.Count > 0)
            {
                ddlPrinter.Items.Clear();
                foreach (DataRow dr in ds.Tables[0].Rows)
                {

                    ListItem li = new ListItem();


                    li.Text = dr[0].ToString();

                    li.Value = dr[0].ToString();
                    ddlPrinter.Items.Add(li);
                }
            }


        }

        protected void rblStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (PrintPageInfo(txtLblUp.Text))
            {
                prtAcc.LblUp(rblStyle.SelectedValue, ddlPrinter.SelectedValue, Convert.ToInt32(txtLblUp.Text));
                txtLblUp.Text = "";
            }

            if (PrintPageInfo(txtLblDown.Text))
            {
                prtAcc.LblDown(rblStyle.SelectedValue, ddlPrinter.SelectedValue, Convert.ToInt32(txtLblDown.Text));
                txtLblDown.Text = "";
            }

            if (PrintPageInfo(txtLblLeft.Text))
            {
                prtAcc.LblLeft(rblStyle.SelectedValue, ddlPrinter.SelectedValue, Convert.ToInt32(txtLblLeft.Text));
                txtLblLeft.Text = "";
            }

            if (PrintPageInfo(txtLblRight.Text))
            {
                prtAcc.LblRight(rblStyle.SelectedValue, ddlPrinter.SelectedValue, Convert.ToInt32(txtLblRight.Text));
                txtLblRight.Text = "";
            }
            if (PrintPageInfo(txtFontSize.Text))
            {
                prtAcc.SetFontSize(rblStyle.SelectedValue, ddlPrinter.SelectedValue, Convert.ToInt32(txtFontSize.Text));
                txtFontSize.Text = "";

            }
            if (ddlFont.SelectedIndex != 0)
            {
                prtAcc.SetFont(rblStyle.SelectedValue, ddlPrinter.SelectedValue, ddlFont.SelectedValue);
                ddlFont.SelectedIndex = 0;
            }
            else
            {
                prtAcc.SetFont(rblStyle.SelectedValue, ddlPrinter.SelectedValue, "宋体");
            }


        }

        protected void btnTestPrint_Click(object sender, EventArgs e)
        {
            if (PrintPageInfo(txtPageUp.Text))
            {
                prtAcc.PageUp(ddlPrinter.SelectedValue, Convert.ToInt32(txtPageUp.Text));
                txtPageUp.Text = "";
            }
            if (PrintPageInfo(txtPageDown.Text))
            {
                prtAcc.PageDown(ddlPrinter.SelectedValue, Convert.ToInt32(txtPageDown.Text));
                txtPageDown.Text = "";
            }
            if (PrintPageInfo(txtPageRight.Text))
            {
                prtAcc.PageRight(ddlPrinter.SelectedValue, Convert.ToInt32(txtPageRight.Text));
                txtPageRight.Text = "";
            }
            if (PrintPageInfo(txtPageLeft.Text))
            {
                prtAcc.PageLeft(ddlPrinter.SelectedValue, Convert.ToInt32(txtPageLeft.Text));
                txtPageLeft.Text = "";
            }



            Page.RegisterStartupScript("printTest", string.Format("<script>window.open('print.aspx?Sturt=Test&priName={0}&per_ID={1}')</script>", ddlPrinter.SelectedValue, Session["Per_ID"].ToString()));
        }

        public bool PrintPageInfo(string str)
        {

            if (str != "")
            {
                if (!MeetingManagerLib.DataTypeCheck.IsDigit(str))
                {
                    Page.RegisterStartupScript("q", "<script>alert('该参数只能是数字！')</script>");
                    return false;
                }
                //if (ddlPrinter.SelectedIndex == 0)
                //{
                //    Page.RegisterStartupScript("w", "<script>alert('无法修改默认！')</script>");
                //    return false;
                //}
                return true;

            }
            else
            {
                return false;
            }

        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            Page.RegisterStartupScript("print", string.Format("<script>window.open('print.aspx?Sturt=Print&priName={0}&per_ID={1}')</script>", ddlPrinter.SelectedValue, Session["Per_ID"].ToString()));

            PermitTable permit = new PermitTable();
            PermitAccess perAcc = new PermitAccess();
            permit.Per_ID = Convert.ToInt32(Session["Per_ID"]);
            permit.Per_Count = 1;
            permit.Per_Printer = ((UserTable)Session["UserTable"]).NAME;
            int count = perAcc.UpdatePermitInfo(permit);
        }
    }
}
